package com.hotel.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.entity.Echarts;
import com.hotel.mapper.EchartsMapper;
import com.hotel.service.EchartsService;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EchartsServiceImpl extends ServiceImpl<EchartsMapper, Echarts> implements EchartsService {

    @Resource
    private EchartsMapper echartsMapper;

    @Override
    public Map<String, Object> getMonthList() {
        Map<String, Object> map = new HashMap<>();
        List<String> roomType = echartsMapper.getRoomType();
        Double[] price = getRoomPrice(roomType, oneDay(), endDay());
        map.put("x", roomType);
        map.put("y", price);
        return map;
    }

    @Override
    public Map<String, Object> getDayList() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String format = formatter.format(date);
        Map<String, Object> map = new HashMap<>();
        List<String> roomType = echartsMapper.getRoomType();
        Double[] price = getRoomPrice(roomType, format, format);
        map.put("x", roomType);
        map.put("y", price);
        return map;
    }

    @Override
    public Map<String, Map<String, Object>> getWeeKList() {
        List<Object> list = new ArrayList<>();
        List<String> roomType = echartsMapper.getRoomType();
        List weekDay = getWeekDay();
        String[] week = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        List<Echarts> monthList = echartsMapper.getMonthList(weekDay.get(0) + "", weekDay.get((weekDay.size() - 1)) + "");
        monthList.forEach(echarts -> {
            echarts.setTotalPrice((echarts.getPrice() * echarts.getDayNumber()));
        });
        for (int i = 0; i < roomType.size(); i++) {
            Map<String, Object> hashMap = new HashMap<>();
            double[] prices = new double[weekDay.size()];
            for (int j = 0; j < weekDay.size(); j++) {
                double itemPrice = 0;
                for (int k = 0; k < monthList.size(); k++) {
                    String[] s = monthList.get(k).getInDate().split(" ");
                    if (s[0].equals(weekDay.get(j)) && monthList.get(k).getRoomName().equals(roomType.get(i))) {
                        itemPrice += monthList.get(k).getTotalPrice();
                    }
                    if (k == monthList.size() - 1) {
                        prices[j] = itemPrice;
                    }
                }
                hashMap.put("name",roomType.get(i));
                hashMap.put("prices", Arrays.stream(prices).toArray());
            }
            list.add(hashMap);
        }
        HashMap<String, Object> roomTypeMap = new HashMap<>();
        roomTypeMap.put("data", roomType);
        HashMap<String, Object> weekMap = new HashMap<>();
        weekMap.put("data", week);
        HashMap<String, Object> seriesMap = new HashMap<>();
        seriesMap.put("series", list);

        Map<String, Map<String, Object>> map = new HashMap<>();
        map.put("legend", roomTypeMap);
        map.put("xAxis", weekMap);
        map.put("option", seriesMap);
        return map;
    }

    private String oneDay() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar firstDay = Calendar.getInstance();
        firstDay.set(Calendar.DAY_OF_MONTH, 1);
        firstDay.set(Calendar.HOUR_OF_DAY, 0);
        firstDay.set(Calendar.MINUTE, 0);
        return simpleDateFormat.format(firstDay.getTime());
    }

    private String endDay() {
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDay = Calendar.getInstance();
        lastDay.add(Calendar.YEAR, 1);
        lastDay.set(Calendar.DAY_OF_MONTH, lastDay.getActualMaximum(Calendar.DAY_OF_MONTH));
        lastDay.set(Calendar.HOUR_OF_DAY, 23);
        lastDay.set(Calendar.MINUTE, 59);
        return simpleDateFormat2.format(lastDay.getTime());
    }

    private List getWeekDay() {
        //获取日历实例，默认日期时间为当前，可根据具体的业务场景修改日期时间
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String format = formatter.format(date);
        String[] split = format.split("-");
        //设置日期为2020-04-08(武汉解封的日子)
        calendar.set(Calendar.YEAR, Integer.parseInt(split[0]));
        calendar.set(Calendar.MONTH, Integer.parseInt(split[1]) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(split[2]));
        /**
         * 设置日历日期为本周的周一
         * 循环取当前的星期和周一做比对，如果不是周一，设置日历往过去推一天，直到设置周一成功
         */
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }

        //当前周日期数组
        String[] currentWeekDays = new String[7];

        //日期格式化
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        currentWeekDays[0] = df.format(calendar.getTime());

        for (int i = 1; i < currentWeekDays.length; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            currentWeekDays[i] = df.format(calendar.getTime());
        }

        return Arrays.asList(currentWeekDays);
    }

    private Double[] getRoomPrice(List<String> roomType, String oneDay, String endDay) {
        List<Echarts> monthList = echartsMapper.getMonthList(oneDay, endDay);
        monthList.forEach(echarts -> {
            echarts.setTotalPrice((echarts.getPrice() * echarts.getDayNumber()));
        });
        System.out.println(monthList);
        Double price[] = new Double[roomType.size()];
        for (int i = 0; i < roomType.size(); i++) {
            double itemPrice = 0;
            for (int i1 = 0; i1 < monthList.size(); i1++) {
                if (monthList.get(i1).getRoomName().equals(roomType.get(i))) {
                    itemPrice += monthList.get(i1).getTotalPrice();
                }
            }
            price[i] = itemPrice;
        }
        return price;
    }


}
